package javaweb.oa.web.action;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javaweb.oa.web.action.Utils.DBUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DeptListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        //获取应用的根路径
        String contextPath = request.getContextPath();

out.print("        <!doctype html>");
out.print("<html>");
out.print("	<head>");
out.print("		<meta charset='utf-8'>");
out.print("		<title>部门列表页面</title>");
out.print("		<style type='text/css'>");
out.print("                table{");
out.print("            height:200px;");
out.print("            width:350px;");
out.print("            border:2px solid pink;");
out.print("            border-collapse:collapse;");
out.print("        }");
out.print("        a{");
out.print("            color:black;");
out.print("            text-decoration:none;");
out.print("        }");
out.print("        a:hover{");
out.print("            color:pink;");
out.print("            text-decoration:underline;");
out.print("            cursor:pointer;");
out.print("        }");
out.print("		</style>");

out.print("<script type='text/javascript'>");
out.print("             function del(deptno){");
out.print("         if(window.confirm('亲，删除了恢复不了哦！')){");
out.print("             alert('正在删除，请稍后~');");

out.print("             document.location.href = '"+contextPath+"/dept/delete?deptno='+deptno;");
out.print("         }");
out.print("     }");
out.print("	</script>");

out.print("		");
out.print("	</head>");
out.print("");
out.print("	<body>");
out.print("		<h1 align='center'>部门列表</h1>");
out.print("		<hr color='green'/>");
out.print("			<table border='1px' align = 'center'>");
out.print("				<tr>");
out.print("					<th>序号</th>");
out.print("					<th>部门编号</th>");
out.print("					<th>部门名称</th>");
out.print("					<th colspan='3'>操作</th>");
out.print("				</tr>");


                Connection conn = null;
        PreparedStatement ps = null;
        ResultSet re = null;
        try{
            conn = DBUtil.getConnection();

            String sql = "select deptno,dname,loc from dept";
            ps = conn.prepareStatement(sql);
            re = ps.executeQuery();

            int seqNum = 1;

            while(re.next()){
                String deptno = re.getString("deptno");
                String dname = re.getString("dname");
                String loc = re.getString("loc");

                out.print("<tr>");
				out.print("	<td align='center'>"+(seqNum++)+"</td>");
				out.print("	<td align='center'>"+(deptno)+"</td>");
				out.print("	<td align='center'>"+(dname)+"</td>");
				out.print("	<td align='center'><a href='javascript:void(0)' onclick='del("+deptno+")'>删除</a></td>");
				out.print("	<td align='center'><a href='"+contextPath+"/dept/edit?deptno="+deptno+"'>修改</a></td>");
				out.print("	<td align='center'><a href='"+contextPath+"/dept/detail?deptno="+deptno+"'>详情</a></td>");
				out.print("</tr>");

            }

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,re);
        }
out.print("       </table>");
out.print("			<hr color='pink'/>");
out.print("			<a href='/oa/add.html'>新增部门</a>");
out.print("	</body>");
out.print("</html>");

    }
}
